package com.szholly.plug.activity.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.szholly.utils.cache.CacheFactory;
import com.szholly.utils.cache.data.IMedataCache;
import com.szholly.data.general.QueryMap;

/**
 * 定义工作流配置缓存
 */
public class BpmConfigCache {
	
	/**
	 * 工作流配置缓存
	 */
	static final String BpmCacheKey = "BpmCacheKey";
	
	/**
	 * 工作流任务配置缓存
	 */
	static final String BpmTaskCacheKey = "BpmTaskCacheKey";
	
	/**
	 * 重新初始化
	 */
	public static void reInit(){
		IMedataCache medataCache = CacheFactory.getMedataCache();
		medataCache.Remove(BpmCacheKey);
		medataCache.Remove(BpmTaskCacheKey);
		init();
	}

	/**
	 * 初始化缓存
	 */
	@SuppressWarnings("unchecked")
	public static void init(){
		IMedataCache medataCache = CacheFactory.getMedataCache();
		Map<String, BpmConfigRow> bpmCache = (Map<String, BpmConfigRow>)medataCache.Get(BpmCacheKey);
		if(bpmCache != null){
			return;
		}
		
		// bpm缓存
		List<BpmConfigRow> bpmRows = BpmConfigTable.getSingleRef().getEntityList(BpmConfigRow.class, new QueryMap());
		bpmCache = new HashMap<String, BpmConfigRow>();
		for(BpmConfigRow row : bpmRows){
			String key = row.getSDefitionid();
			bpmCache.put(key, row);
		}
		medataCache.Put(BpmCacheKey, bpmCache);
		
		List<BpmTaskConfigRow> bpmTaskRows = BpmTaskConfigTable.getSingleRef().getEntityList(BpmTaskConfigRow.class, new QueryMap());
		Map<String, Map<String, BpmTaskConfigRow>> bpmTaskCache = 
				new HashMap<String, Map<String, BpmTaskConfigRow>>();
		for(BpmTaskConfigRow taskRow : bpmTaskRows){
			String key = taskRow.getSDefitionid();
			String taskId = taskRow.getSTaskid();
			
			Map<String, BpmTaskConfigRow> taskCache = null;
			if(bpmTaskCache.containsKey(key)){
				taskCache = bpmTaskCache.get(key);
			} else {
				taskCache = new HashMap<String, BpmTaskConfigRow>();
				bpmTaskCache.put(key, taskCache);
			}
			
			taskCache.put(taskId, taskRow);
		}
		medataCache.Put(BpmTaskCacheKey, bpmTaskCache);
	}

	/**
	 * 根据定义ID，获取流程配置
	 * @param dId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static BpmConfigRow getConfig(String dId){
		init();
		IMedataCache medataCache = CacheFactory.getMedataCache();
		Map<String, BpmConfigRow> bpmCache = (Map<String, BpmConfigRow>)medataCache.Get(BpmCacheKey);
		if(bpmCache==null){
			return null;
		}
		if(bpmCache.containsKey(dId)){
			return bpmCache.get(dId);
		}else{
			return null;
		}
	}
	
	/**
	 * 根据定义ID和任务ID，获取配置
	 * @param dId
	 * @param taskId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static BpmTaskConfigRow getTaskConfig(String dId, String taskId){
		init();
		IMedataCache medataCache = CacheFactory.getMedataCache();
		Map<String, Map<String, BpmTaskConfigRow>> bpmTaskCache = 
				(Map<String, Map<String, BpmTaskConfigRow>>)medataCache.Get(BpmTaskCacheKey);
		if(bpmTaskCache==null){
			return null;
		}
		if(bpmTaskCache.containsKey(dId)){
			Map<String, BpmTaskConfigRow> taskCache = bpmTaskCache.get(dId);
			if(taskCache.containsKey(taskId)){
				return taskCache.get(taskId);
			}else{
				return null;
			}
		}else{
			return null;
		}
	}
	
	/**
	 * 根据定义ID和任务ID，获取配置
	 * @param dId
	 * @param taskId
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, BpmTaskConfigRow> getTasksConfig(String dId){
		init();
		IMedataCache medataCache = CacheFactory.getMedataCache();
		Map<String, Map<String, BpmTaskConfigRow>> bpmTaskCache = 
				(Map<String, Map<String, BpmTaskConfigRow>>)medataCache.Get(BpmTaskCacheKey);
		if(bpmTaskCache==null){
			return null;
		}
		if(bpmTaskCache.containsKey(dId)){
			Map<String, BpmTaskConfigRow> taskCache = bpmTaskCache.get(dId);
			return taskCache;
		}else{
			return null;
		}
	}
}